<?php

/*
    Open Designs Content Management System (odCMS)
    Copyright (C) 2010  Thomas Wong - tom@ztml.org

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
*/

class contentControl extends io

{

	/*---------------------------\
	| initialize content objects |
	\---------------------------*/
	function contentControl()
	{
		if($_REQUEST['vfy_image']) return $this->vfyImage();
	
		$this->init();

		if($this->_CC_['Update']) return;

		if($_GET['generate'] == "html")
		{
			return $this->generateHTML();
		}
		
		$this->Page = "";
		$this->opUdfMsgs( "template: ".$this->_CC_['template'] );
	}

	/*-------------------\
	| verification image |
	\-------------------*/
	function vfyImage()
	{
		header('Content-type: image/jpeg');

		$width  = 50;
		$height = 24;

		$my_image = imagecreatetruecolor($width, $height);

		imagefill($my_image, 0, 0, 0xFFFFFF);

                // add noise
		for ($c = 0; $c < 40; $c++)
		{
			$x = rand(0,$width-1);
			$y = rand(0,$height-1);
			imagesetpixel($my_image, $x, $y, 0x000000);
		}

		$x = rand(1,10);
		$y = rand(1,10);

		$rand_string = rand(1000,9999);
		imagestring($my_image, 5, $x, $y, $rand_string , 0x000000);
		$_SESSION['rand_number'] = md5($rand_string).'a4xn';

		imagejpeg($my_image);
		imagedestroy($my_image);

		exit;
	}
	
	/*-----------------------------\
        | output user defined messages |
        \-----------------------------*/
        function opUdfMsgs($tpl)
        {
                $utpl = explode("\n",$tpl);

                $size = count($utpl);

                for($ix=0;$ix<$size;$ix++)
                {
			$utpl[$ix] = rtrim($utpl[$ix]);
                        if(!$utpl[$ix]) continue;

			if($this->procUsrDefined($utpl[$ix])) continue;

			$key = $this->_CC_['_key_'];
			$val = $this->_CC_['_val_'];
			
                        switch($key)
                        {
                                case 'template':
					if($this->_CC_['theme']) $val=$this->_CC_['theme'];
					
					$this->getTemplate($val);
					$this->procTemplate();
					if($_REQUEST['test']=="timing")
					{
						$jt = round($this->getMicrotime()-$this->start_time, 4);
						echo "Job time: $jt ($val) ($this->cwd)";
					}
                                        break;

                                case 'use':
					$this->useTemplate($val);
                                        break;

                                case 'opdata':
					$this->opFormData($val);
                                        break;

                                case 'opmsg':
					$this->opMessage($val);
                                        break;

                                case 'opform':
					$this->opForm($val);
                                        break;
					
                                case 'getform':
					$this->_CC_['input_form'] = $this->getForm($val);
                                        break;

                                case 'get':
					if($this->_CC_['Page'] == "_CC_") break;

					if($this->_CC_['Page']!="login")
					{
						$this->getContent($val);
						break;
					}

					if($_SESSION['CWD'])
					{
						$this->_CC_['directory'] = $_SESSION['CWD'];
						$this->_CC_['session'] = $_SESSION['UserName'];
						$this->_CC_['content'] = $this->getUdfMsg("udf_067"); // session active
					}
				        else
					{
						$this->_CC_['content'] = $this->getTplValue("frms","login");
					}
                                        break;

                                case 'set':
                                        $val = $this->subString($val);
                                        $this->setUdfList($val);
                                        break;
					
                                case 'str':
                                        list($key,$val) = explode("=",$this->_CC_['_reg_'],2);
                                        $key = trim($key);
                                        $this->_CC_[$key] = $val;
                                        break;

                                case 'menu':
                                        $this->getMenu($val);
                                        break;
					
                                case 'tree':
					list($page,$stag,$pattern,$etag) = explode("^^",$val);
					$this->_CC_['tree'] = "";
                                        $this->getMenuTree($page,$stag,$pattern,$etag,0);
                                        break;

                                case 'document':
					$this->getDocument($val);
					break;
					
                                case 'exec':
                                        list($func,$parms) = explode(':',$val);
					$parms = explode(',',$parms);
					$this->$func($parms[0],$parms[1],$parms[2]);
                                        break;
					
                                case 'add':
					$val = $this->subString($val);
					list($a,$b) = explode(",",$val);
					$sum = $a+$b;
					printf("%d",$sum);
                                        break;

                                case 'sub':
					$val = $this->subString($val);
					list($a,$b) = explode(",",$val);
					$sub = $a-$b;
					printf("%d",$sub);
                                        break;

				case 'dump':
					echo "<font color='navy' size='4'>_CC_ object values:</font><p><pre>";
					print_r($this->_CC_);
					echo "</pre>";
					break;
					
                                case 'gallery':
					$val = $this->subString($val);
					$this->getPhotos($val);
					break;
					
                                case 'image':
					$val = $this->subString($val);
					$this->getImage($val);
					break;
					
				case 'subs': $val = $this->subString($val);
				case 'echo':
					if($_GET['generate']) $this->Page .= $val;
					echo "$val\n";
                                        break;
					
                                case 'themes':
					$this->getSelectList("default","default");

                                        break;
					
                                case 'end':
					echo $val;
					if($_GET['generate']) $this->Page .= $val;
					$this->savePage();
                                        exit;
					
                                case 'random':
					list($a,$b) = explode(",",$val);
					$this->_CC_['random'] = rand($a,$b);
                                        break;

				default:
					if(!$this->_CC_['member']) $this->_CC_['member']= "admin";
					$val = $this->subString($utpl[$ix]);
					echo "$val\n";
					if($_GET['generate']) $this->Page .= "$val\n";
                        }
                }
                return;
        }

        /*-----------------------------\
        | process user defined section |
        \-----------------------------*/
        function procUsrDefined($utpl)
        {
		if($utpl[0]=="/" && $utpl[1]=="/") return 1; // skip comment

		if(preg_match("/^\?\s*([\ \$\_\.0-9a-zA-Z]*)\s*(\<|\>|\!|=)\s*([\ \_\.0-9a-zA-Z]*)\s+(\w+)\s*\:\s*(.*)/",$utpl,$regs))
		{
			$skip = 0;
			$left = $regs[1];
			$op   = $regs[2];
			$right= $regs[3];
			$key  = $regs[4];
			$val  = $regs[5];
			$tmp  = $regs[6];

			if($left == "username") $left = $this->_CC_['username'];
			switch($op)
			{
					// values are equal
				case '=':
					if($right=="blank")
					{
						if($this->_CC_[$left]) $skip++;
					}
					else if(!preg_match("/^$left$/i",$this->_CC_[$right])) $skip++;

					break;

					// values are not equal
				case '!':
					if($right=="blank")
					{
						if(!$this->_CC_[$left]) $skip++;
					}
					else if( preg_match("/^$left$/i",$this->_CC_[$right])) $skip++;
					break;

					// value1 is greater than value2
				case '>':
					if($left<$right) $skip++;
					break;

					// value1 is less than value2
				case '<':
					if($left>$right) $skip++;
					break;

				default: $skip++;
			}
			if($skip) return 1;
			$this->_CC_[$key] = $key;
			$val = $regs[5];
		}
		elseif(preg_match("/^(\w+)\:\s*(.*)/",$utpl,$regs))
		{
			$key = $regs[1];
			$val = $regs[2];
			$this->_CC_[$key] = $val;
		}
		$this->_CC_['_key_'] = $key;
		$this->_CC_['_val_'] = $val;
		$this->_CC_['_reg_'] = $regs[2];

		return 0;
	}

        /*-----------------\
        | process template |
        \-----------------*/
	function procTemplate()
	{
		if($this->_CC_header) $this->opUdfMsgs($this->_CC_header);
		if($this->_CC_body) $this->opUdfMsgs($this->_CC_body);

		$this->loadToolBox();
	}

		
        /*------------\
        | get content |
        \------------*/
	function getContent($val)
	{
		list($val,$type) = explode(":",$val);
		
		if($val=="attachment" && $type=="sections")
		{
			$this->getDocSections($val);
			return;
		}
		if(!$this->_CC_['title'])
		{
			$this->_CC_['title'] = $this->_CC_['uname'].": ".$this->_CC_['Page']." | ".
					       $this->_CC_['cname']." - ".$this->_CC_['slogan'];
		}
		
		if($this->_CC_['Page']=="_menus_")	return $this->updateMenus();
		if($this->_CC_['Page']=="_sections_")	return $this->updateSections();
		if($this->_CC_['Page']=="_profile_")	return $this->updateProfile();
		if($this->_CC_['Page']=="_admin_")	return $this->manageUsersTemplates();
		if($this->_CC_['Page']=="_users_")	return $this->manageUsers();
		if($this->_CC_['Page']=="_templates_")	return $this->manageTemplates();
		if($this->_CC_['Page']=="_sitemap_")	return $this->createSiteMap();
		if($this->_CC_['Page']=="_members_")	return $this->getMembers("");
		if($this->_CC_['Page']=="_designs_")	return $this->getDesigns("wfiedler",0);
		if($this->_CC_['Page']=="_designer_")	return $this->getDesigner();
		if($this->_CC_['Page']=="_mail_")	return $this->dispMailMsgs("wdn_mails");
		if($this->_CC_['Page']=="_listing_")	return $this->getListings("update_listing");
		if($this->_CC_['Page']=="_cpanel_")	return $this->controlPanel();

		$pname = $this->subString($val);
		
		if($this->_CC_['TYPE']=="Blog")
		{
			$this->_CC_['header'] = $pname;
			$this->_CC_['content'] = "";
			list($pname) = explode(".",$pname);
			$this->getBlog($pname);
			if($this->_CC_['admin'])
			{
				$this->_CC_['header'] = "Latest News and Activities";
				$this->_CC_['member_latest'] .= $this->_CC_['content'];
				$this->_CC_['content'] = "";
				return;
			}
			return;
		}
		if($this->_CC_['TYPE']=="Activity")
		{
			$this->_CC_['header'] = "Recent Activities";
			$this->_CC_['content'] = "";
			$this->getActivities($pname);
			if($this->_CC_['admin'])
			{
				$this->_CC_['header'] = "Latest News and Activities";
				$this->_CC_['member_latest'] .= $this->_CC_['content'];
				$this->_CC_['content'] = "";
				return;
			}
			return;
		}
		$flag = 0;
		if($val=="{Page}")
		{
			switch($this->_CC_['TYPE'])
                        {
                                case 'Gallery': $this->procGallery($pname); $flag = 1; break;
				case 'Forum':	$this->procForum($pname); $flag = 1; break;
                                case 'Table':	$this->dispTable($pname); $flag = 1; break;
				case 'Listings':$this->showListings($pname); $flag = 1; break;
                                case 'Local':	$this->showLocalContent($pname); $flag = 1; break;
			}
		}
		if(!$flag)
		{
			if($this->getUserContent($pname,"getContent")) return;

			if($_REQUEST['print'] || $_GET['generate'])
			{
				return;
			}
		}
		if($this->username==$_SESSION['UserName'] && $this->cwd == $_SESSION['CWD'])
		{
			$elink = "";
			if(!$flag) $elink = "<a class='thickbox' href='?Update=content&Page=$pname".
				 "TB_iframe=true&height=470&width=680' title='Edit this page ($pname)'>".
				 "<img border='0' src='../templates/img/pencil.png' /></a>";
			$elink .= "<a class='thickbox' href='?Update=menu&menu=".$this->_CC_['MENU']."&edit=".$this->_CC_['MID'].
				  "&lytebox=1TB_iframe=true&height=470&width=680' title='Edit this menu ($pname)'>".
				  "<img border='0' src='../templates/img/editmenu.gif' alt='EditMenu' /></a>";
			$this->_CC_['content'] = $elink.$this->_CC_['content'];
		}

	}
	
        /*----------------------\
        | get document contents |
        \----------------------*/
        function getDocument($dname)
        {
		if($dname != "attachment")
		{
			echo "Document type: $dname is not supported";
			exit;
		}
		$this->_CC_['contents'] = "";
		$this->_CC_['count'] = $snum = 0;

		$q = @$this->UDB->query("SELECT * FROM Document WHERE Type='Attachment' ORDER BY cid");
		if ($q === false)
		{
			return;
		}
		while( $q->valid() )
		{
			list($title,$cid,$type,$header,$content) = $q->current();
			$header = trim($header);
			$content = trim($content);

			$this->_CC_['contents'] .= "<li><strong>$header</strong>".
				  "<input type='checkbox' class='checkbox' name='delete_$snum' value='$cid' /> Delete".
				  " <a href='?Update=content&document=$cid&flag=0'>Edit</a></li>\n";
			$this->_CC_['count']++;
			$snum++;

			$q->next();
		}
		return;
        }
	
        /*---------\
        | get blog |
        \---------*/
	function getBlog($pname)
	{
		$uname = $this->username;
		$this->username = $this->_CC_['member'];

		$save = $this->UDB;
		$this->UDB  = new SQLiteDatabase("./users/$this->username/contents.dbf"); // user content database
		$this->getProfile();
		$this->UDB = $save;

		$this->username = $uname;
		
		$this->getBlogContent($pname);

		if($pname=="News") $template = "recent_news";
		else $template = "blog_content";
		list($header,$body,$footer) = explode("^^^", $this->getTplValue("msgs",$template));

		$this->_CC_['content']  = $this->subString($header);
		$this->_CC_['content'] .= $this->subString($body);

		if($this->_CC_['member']==$_SESSION['UserName'] && $this->cwd == $_SESSION['CWD'])
		{
			if(!$this->_CC_['admin']) $this->_CC_['content'] .= $this->getUdfMsg("udf_085"); // Update links
		}
		$this->_CC_['content'] .= $this->subString($footer);
		
		if($pname!="News")
		{
			if($this->_CC_['vfy'] == 1) $this->_CC_['wrong_code'] = $this->getUdfMsg("udf_065"); // Missing required info
			else if($this->_CC_['vfy'] == 2) $this->_CC_['wrong_code'] = $this->getUdfMsg("udf_066"); // Wrong verification code

			$this->getBlogComments($pname);
		}
		$this->_CC_['content'] .= $this->_CC_['comments'];
	}
	
        /*-------------\
        | get listings |
        \-------------*/
	function getListings($template)
	{
		$this->getBlogContent("_listings_");

		list($this->_CC_['summary'],$this->_CC_['description']) = explode("^^^",$this->_CC_['this_content']);
		$this->_CC_['ltitle']  = $this->_CC_['this_topic'];
		$this->_CC_['credits'] = $this->_CC_['this_website'];
		
		if($_SESSION['UserName'] == $this->_CC_['member'] && $this->cwd==$_SESSION['CWD'])
		{
			$this->_CC_['update'] = "<a href='?Page=My Announcement&bid=".$this->_CC_['this_bid']."&edit=1'>Update Announcement</a>";
		}

		if($this->_CC_['edit']) $template = "update_listing";
		if(!$this->_CC_['this_bid'] && $_SESSION['UserName']) $template = "add_listing";
		$this->getMemberProfile("");
		list($header,$body,$footer) = explode("^^^", $this->getTplValue("msgs",$template));
		if($template=="add_listing")
		{
			if($this->_CC_['this_bid'])
			{
				$errmsg = $this->getUdfMsg("udf_090");
				return $this->subString($header).$errmsg.$this->subString($footer);
			}
		}
		return $this->subString($header).$this->subString($body).$this->subString($footer);
	}

	/*---------------------------------\
        | substitute form with user string |
        \---------------------------------*/
        function subString($ustr)
        {
                $ustr = preg_replace("/{(.*?)}/e","\$this->patReplace('\\1')",$ustr);
                $ustr = preg_replace("/&#123;/","{",$ustr);

                return $ustr;
        }

        /*-------------------------------\
        | replace pattern with user data |
        \-------------------------------*/
        function patReplace($pat)
        {
                $functions = array(
                        "date"          => "patDate",
                        "use"           => "opUdfMsgs",
                        "set"           => "setUdfList",
                        "image"		=> "getImage",
                        "gallery"	=> "getPhotos",
                        "photo"		=> "getPhotos",
                        "activities"	=> "getActivities",
                        "listings"	=> "getListings",
                        "members"	=> "getMembers",
                        "designs"	=> "getDesigns",
                        "profile"	=> "getMemberProfile",
			"mail"		=> "dispMailMsgs",
			"stat"		=> "getMemberStatistics"
                );
		
                if(is_array ($this->_CC_[$pat])) return $this->getUdfMsg("udf_061"); // INVALID VALUE

                if(preg_match("/^img=/",$this->_CC_[$pat])) $pat = $this->_CC_[$pat];
                if(preg_match("/^(\w+)\s*=\s*(.*)/",$pat,$regs))
                {
                        if($functions[$regs[1]])
			{
				if($regs[1]=="use") $param = $this->getTplValue("msgs",$regs[2]);
				else $param = $regs[2];
				return $this->$functions[$regs[1]]($param);
			}
                }
                list($pat,$attrs) = explode(":",$pat);
		
                if(isset($this->_CC_[$pat])) return $this->_CC_[$pat];

                if(!preg_match("/^\w+$/",$pat))
                {
                        list($pat,$ext) = explode("$",$pat);
                        if($ext)
                        {
                                $val = $this->_CC_[$ext];
                                if(!$val) $val = $ext;
                                if($this->_CC_[$pat]) $pat = $this->_CC_[$pat];
                                $pat = "\{$pat$val}";
                                return preg_replace("/{(.*?)}/e","\$this->patReplace('\\1')",$pat);
                        }
                        return "&#123;$pat}";
                }
                return "";
        }
	
        /*---------------------\
        | process date pattern |
        \---------------------*/
        function patDate($dpat)
        {
                list($fmt,$dtime) = explode("^",$dpat);
                $fmt = trim($fmt);

		$dtime = $this->_CC_[$dtime];
                if(!$dtime)
		{
			$dtime = time();
		}
                else
                {
                        $dtime = preg_replace("/\:|\-|\ /","",$dtime);

                        list($y,$m,$d,$h,$mi,$s) = sscanf($dtime, "%4d%2d%2d%2d%2d%2d");

                        $dtime = mktime($h,$mi,$s,$m,$d,$y);
                }

                $this->_CC_['date'] = date ($fmt,$dtime);

                return $this->_CC_['date'];
        }

	/*------------------------\
        | set user defined object |
        \------------------------*/
        function setUdfList($val)
        {
		list($key,$val) = explode("=",$val,2);
		$val = $this->subString($val);
		$key = trim($this->subString($key));
		if($key=="ctitle")
		{
			if($val) $this->_CC_['title'] = "$val - ".$this->_CC_['title'];
		}
		else if($key=="SID")
		{
			if($_SESSION['SID']) $this->_CC_['SID'] = $_SESSION['SID'];
			else $this->_CC_['SID'] = $val;
			return;
		}
		$this->_CC_[$key] = $val;

		return;
	}
	
	/*----------------\
        | create site map |
        \----------------*/
	function createSiteMap()
	{
		$this->_CC_['menu_list']  = array();
		
		$this->_CC_['content'] = $this->_CC_['sitemap_stag'];
		$this->_CC_['ID'] = -1;
		$this->_CC_['header'] = "Site Map";

		$this->_CC_['maxLevels'] = 5;
		
		$this->_CC_['tree'] = "<ul>";
		$this->getMenuTree("menu1","<li>",
				   "<a href='{URL}' title='{TITLE}' {ATTRS}>{NAME}</a>","</li>",0);
		$this->_CC_['content'] .= "<div  style='padding:20px;'>".$this->_CC_['tree'];
		$this->_CC_['tree'] = "";
		$this->getMenuTree("menu2","<li>",
				   "<a href='{URL}' title='{TITLE}' {ATTRS}>{NAME}</a>","</li>",0);
		$this->_CC_['content'] .= $this->_CC_['tree'];
		$this->_CC_['tree'] = "";
		$this->getMenuTree("menu3","<li>",
				   "<a href='{URL}' title='{TITLE}' {ATTRS}>{NAME}</a>","</li>",0);
		
		$this->_CC_['content'] .= $this->_CC_['tree']."</ul><br /></div>".$this->_CC_['sitemap_etag'];
	}
	
	/*--------------\
        | get menu tree |
        \--------------*/
	function getMenuTree($page,$stag,$pattern,$etag,$flag)
	{
		$size = $this->_CC_[$page."Size"];

		if(!$size) return;

		$sTag = $this->_CC_['mTag1'];
		$eTag = $this->_CC_['mTag2'];
		if(!$sTag)
		{
			$sTag = "<ul>";
			$eTag = "</ul>";
		}
		$maxLvls = $this->_CC_['maxLevels'];
		if(!$maxLvls) $maxLvls = 5;
                for($ix=0;$ix<$size;$ix++)
                {
			$this->_CC_['TITLE'] = trim($this->_CC_[$page."Title_$ix"]);
			$this->_CC_['ATTRS'] = trim($this->_CC_[$page."Attrs_$ix"]);
			$this->_CC_['HIDE']  = $this->_CC_[$page."Hide_$ix"];
			$this->_CC_['MAIN']  = $this->_CC_[$page."Main_$ix"];
			$type = $this->_CC_[$page."Type_$ix"];
			
			$this->_CC_['NAME'] = $name = trim($this->_CC_[$page."Name_$ix"]);
			if($this->_CC_['Page']==$this->_CC_['NAME'])
			{
				if($type!="Forum") $this->_CC_['URL'] = "#";
				$this->_CC_['SELECTED'] = $this->_CC_['tree_selected'];
				$this->_CC_['OPEN'] = $this->_CC_['mOpen'];
			}
			else
			{
				$this->_CC_['SELECTED'] = $this->_CC_['OPEN'] = "";
			}

			$this->_CC_['NUM'] = $ix;
			$this->_CC_['SIZE']  = $size;
			$this->_CC_['URL'] = "?Page=$name";
			if($this->_CC_['MAIN'])
			{
				if($this->_CC_['MAIN']!="menu1" && $this->_CC_['MAIN']!="menu2" && $this->_CC_['MAIN']!="menu3")
				{
					$this->_CC_['URL']  .= ".".$this->_CC_['MAIN'];
					$name .= ".".$this->_CC_['MAIN'];
				}
			}
			if($_GET['generate'])
			{
				if($type != "Document") continue;
			}
			if($this->_CC_['HIDE'])
			{
				if( !$_SESSION['UserName'] || $_SESSION['CWD']!=$_SESSION['usite'] ) continue;
				//if($this->username!=$_SESSION['UserName'] && $this->cwd == $_SESSION['CWD']) continue;
				$this->_CC_['NAME'] = "<i>".$this->_CC_['NAME']."</i>";
				//$this->_CC_['URL']  = "#";
			}
			else
			{
				//array_push($this->_CC_['menu_list'],$name);
			}
			if($flag)
			{
				if($type=="Blog")
				{
					if($this->_CC_['TITLE']) $this->_CC_['NAME'] = $this->_CC_['TITLE'];
				}
			}

                        $this->_CC_['tree'] .= $stag.$this->subString($pattern)."\n";
			$this->getMenuList($name,$name);
			if($flag <$maxLvls && $this->_CC_[$name."Size"] && $type!="Gallery")
			{
				$this->_CC_['tree'] .= $this->subString($sTag);
				$this->getMenuTree($name,$stag,$pattern,$etag,$flag+1);
				$this->_CC_['tree'] .= $eTag;
			}
			$this->_CC_['tree'] .= $etag;
		}
	}
	
	/*---------------\
        | get menu items |
        \---------------*/
	function getMenu($pat)
	{
		list($page,$mask) = explode("^^",$pat);

		if($page == "myMenu")
		{
			$this->_CC_['myMenu'] = "";
			$this->getMenuItems($this->_CC_['myPage'],"myMenu");
		}
                $this->formatMenu($page,$mask);
	}
	
	/*---------------\
        | get menu items |
        \---------------*/
	function formatMenu($page,$pattern)
	{
		$this->_CC_[$page] = "";

		$this->_CC_['SIZE']  = $size = $this->_CC_[$page."Size"];

                for($ix=0;$ix<$size;$ix++)
                {
			$this->_CC_['TITLE'] = trim($this->_CC_[$page."Title_$ix"]);
			$this->_CC_['ATTRS'] = trim($this->_CC_[$page."Attrs_$ix"]);
			$this->_CC_['NAME']  = $name = trim($this->_CC_[$page."Name_$ix"]);
			$this->_CC_['HIDE']  = $this->_CC_[$page."Hide_$ix"];
			$this->_CC_['MAIN']  = $this->_CC_[$page."Main_$ix"];
			$this->_CC_['Desc']  = $this->_CC_[$page."Desc_$ix"];
			$type = $this->_CC_[$page."Type_$ix"];

			if($this->_CC_['NAME'] == "") continue;

			if($this->_CC_['Page']==$this->_CC_['NAME'] && $type!="Forum")
			{
				//$this->_CC_['URL'] = "#";
				$this->_CC_['URL'] = "?Page=".$this->_CC_['NAME'];
				$this->_CC_['SELECTED'] = $this->_CC_[$page.'_selected'];
			}
			else
			{
				$this->_CC_['URL'] = "?Page=".$this->_CC_['NAME'];
				if($page!="menu1" && $page!="menu2" && $page!="menu3")
				{
					if($this->_CC_['MAIN'])
					{
						$this->_CC_['URL']  .= ".".$this->_CC_['MAIN'];
						$name .= ".".$this->_CC_['MAIN'];
					}
				}
				$this->_CC_['SELECTED'] = "";
			}
			if($_GET['generate'])
			{
				if($type != "Document") continue;
			}
			if($this->_CC_['HIDE'])
			{
				if( !$_SESSION['UserName'] || $_SESSION['CWD']!=$_SESSION['usite'] ) continue;
				//if($this->username!=$_SESSION['UserName'] && $this->cwd == $_SESSION['CWD']) continue;
				$this->_CC_['NAME'] = "<i>".$this->_CC_['NAME']."</i>";
				//$this->_CC_['URL']  = "#";
			}
			else
			{
				array_push($this->_CC_['menu_list'],$name);
			}
			$this->_CC_['NUM'] = $ix+$this->_CC_['OFFSET'];
			if($type=="Blog")
			{
				if($this->_CC_['MAIN'])
				{
					if($this->_CC_['TITLE']) $this->_CC_['NAME'] = $this->_CC_['TITLE'];
				}
			}
                        $this->_CC_[$page] .= $this->subString($pattern)."\n";
		}
	}
	
        /*------------------------\
        | get update profile form |
        \------------------------*/
        function getProfileForm()
        {
		if(!$_SESSION['UserName'])
		{
			$this->_CC_['content'] =  "You must log in for this operation.";
			return;
		}
		$this->_CC_['content'] = $this->getForm("update_profile");
		return;
	}

	/*-------------------\
        | get member profile |
        \-------------------*/
        function getMemberProfile($template)
        {
		$this->username = $this->_CC_['member'];
		if($template=="update_profile")
		{
			$this->username = $_SESSION['UserName'];
			if(!$this->username)
			{
				$this->_CC_['content']  = "You must log in for this operation.";
				return;
			}
		}
		$save = $this->UDB;
		
		$this->_CC_['upgrade'] = "";
		$this->UDB  = new SQLiteDatabase("./users/$this->username/contents.dbf"); // user content database
		$this->getProfile();
		$this->getSelectList($this->_CC_['editor'],$this->_CC_['template']);
		
		$this->UDB = $save;

		if(!$template) return;
		
		if($template=="update_profile") return $this->getForm($template);

		list($header,$body,$footer) = explode("^^^", $this->getTplValue("msgs",$template));

		$this->_CC_['content'] = $header.$this->subString($body).$footer;
		
		return;
	}
	
        /*----------------\
        | process gallery |
        \----------------*/
	function procGallery($name)
	{
		$this->_CC_['header']  = $name;

		$gtpl = $this->getTplValue("msgs",$this->_CC_['gtpl']);
		if(!$gtpl) $gtpl = $this->_CC_['pdesc'];

		if(!$gtpl)
		{
			$this->_CC_['content'] = $this->subString($this->getTplValue("msgs","gallery_content"));
		}
		else
		{
			$this->opUdfMsgs($gtpl);
			$this->_CC_['content'] .= $this->_CC_['g_content'];
		}
	}
	
        /*-------------\
        | resize image |
        \-------------*/
        function resizeImage($imgFile,$newsize,$full,$thumb,$ftype)
        {
		list($opt1,$opt2) = split('x',$newsize);

		list($width,$height,$type,$attr) = GetImageSize($imgFile);

		if($type==IMAGETYPE_JPEG)	$img = @imagecreatefromjpeg($imgFile);
		elseif($type==IMAGETYPE_GIF)	$img = @imagecreatefromgif ($imgFile);
		elseif($type==IMAGETYPE_PNG)	$img = @imagecreatefrompng ($imgFile);
		elseif($type==IMAGETYPE_WBMP)	$img = @imagecreatefromwbmp($imgFile);
		else
		{
			if($this->_CC_['url']) $fname = $this->_CC_['url'];
			else $fname = $_FILES['file']['name'];
			list($this->_CC_['fname'],$this->_CC_['ftype']) = explode(".",$fname);

			$this->opUdfMessage("udf_051"); // type not supported
			exit;
		}

		if(!$img)
		{
			$this->opUdfMessage("udf_052"); // Unable to create image
			echo "1: type=($type) resizeImage ($imgFile) ($newsize) ($full) ($thumb) ($ftype)";
			exit;
		}

		if(!$opt1 || $opt1>$width)
		{
			$opt1 = $width;
			$opt2 = $height;
		}

		/*if($opt1 > $width)
		{
			$new1 = $new2 = $img;
			$resize = 0;
		}*/
		$opt2 = $opt1/$width * $height;
		$new1 = imagecreatetruecolor($opt1,$opt2);
		imagecopyresized ( $new1, $img, 0, 0, 0, 0, $opt1,$opt2,$width,$height);
	
		if($width<100) $opt1 = $width;
		else $opt1 = 100;
		
		$opt2 = $opt1/$width * $height;
		$new2 = imagecreatetruecolor($opt1,$opt2);
		imagecopyresized ( $new2, $img, 0, 0, 0, 0, $opt1,$opt2,$width,$height);
		
		$stat = 0;
		if($type==IMAGETYPE_JPEG)
		{
			$stat = @ImageJpeg($new2,"$thumb.jpg");
			if($ftype)
			{
				$full .= ".jpg";
				$thumb .= ".jpg";
				$this->_CC_['fname'] .= ".jpg";
			}
			$stat = @ImageJpeg($new1,$full);
			$stat = @ImageJpeg($new2,$thumb);
		}
	        elseif($type==IMAGETYPE_GIF)
		{
			$stat = @ImageGif ($new2,"$thumb.jpg");
			if($ftype)
			{
				$full .= ".gif";
				$thumb .= ".gif";
				$this->_CC_['fname'] .= ".gif";
			}
			$stat = @ImageGif ($new1,$full);
			$stat = @ImageGif ($new2,$thumb);
		}
	        elseif($ftype)
		{
			$stat = @ImagePng ($new2,"$thumb.jpg");
			if($ftype)
			{
				$full .= ".png";
				$thumb .= ".png";
				$this->_CC_['fname'] .= ".png";
			}
			$stat = @ImagePng ($new1,$full);
			$stat = @ImagePng ($new2,$thumb);
		}
                elseif($ftype)
		{
			$stat = @ImageWbmp($new2,"$thumb.jpg");
			if($ftype)
			{
				$full .= ".wbmp";
				$thumb .= ".wbmp";
				$this->_CC_['fname'] .= ".wbmp";
			}
			$stat = @ImageWbmp($new1,$full);
			$stat = @ImageWbmp($new2,$thumb);
		}
		if(!$stat)
		{
			$this->opUdfMessage("udf_052"); // Unable to create image
			echo "2: resizeImage($imgFile,$newsize,$full,$thumb,$ftype)";
			exit;
		}
		@imagedestroy($new1);
		@imagedestroy($new2);
		@imagedestroy($img);

		return 1;
	}

	/*--------------\
        | control panel |
        \--------------*/
	function cPanel()
	{
		if($this->username!=$_SESSION['UserName'] && $this->cwd == $_SESSION['CWD'])
		{
			return;
		}
		
		if($this->_CC_['USITE']=="_members" && $this->username!="admin") return;
		
		return "<a href='?Page=_cpanel_' title='Control Panel'><img border='0' src='../templates/img/administration.png' /></a>";
	}
	
	/*--------------\
        | load tool box |
        \--------------*/
	function loadToolBox()
	{
		if($this->_CC_['TYPE']=="Gallery") return $this->opMessage("load_toolbox");

		if($_GET['generate']) return;
		
		if($this->_CC_['NO_LOAD'] || $this->_CC_['preview'] || $this->username!=$_SESSION['UserName'] || $this->cwd != $_SESSION['CWD'])
		{
			return;
		}

		return $this->opMessage("load_toolbox");
	}
		
        /*-----------\
        | testing... |
        \-----------*/
        function testing($val)
        {
		echo "($val) This is a test...";
	}
}
?>
